package com.pybox.spec;

import com.pybox.entity.TSession;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
* 描述：TSession Specification
* @author wuyingkai
* @date 2018-09-15
*/


public class TSessionSpec implements Specification<TSession> {

    private Map<String, String> params;

    public TSessionSpec(Map<String, String> params){
        this.params = params;
    }

    @Override
    public Predicate toPredicate(Root<TSession> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {

        String sessionId = this.params.get("sessionId");
        String loginName = this.params.get("loginName");
        String deptName = this.params.get("deptName");
        String ipaddr = this.params.get("ipaddr");
        String loginLocation = this.params.get("loginLocation");
        String browser = this.params.get("browser");
        String os = this.params.get("os");
        String status = this.params.get("status");
        String startTimestamp = this.params.get("startTimestamp");
        String lastAccessTime = this.params.get("lastAccessTime");
        String expireTime = this.params.get("expireTime");
        List<Predicate> ps = new ArrayList<>();

        if (!StringUtils.isEmpty(sessionId)) {
            ps.add(criteriaBuilder.like(root.get("sessionId"), "%" + sessionId + "%"));
        }
        if (!StringUtils.isEmpty(loginName)) {
            ps.add(criteriaBuilder.like(root.get("loginName"), "%" + loginName + "%"));
        }
        if (!StringUtils.isEmpty(deptName)) {
            ps.add(criteriaBuilder.like(root.get("deptName"), "%" + deptName + "%"));
        }
        if (!StringUtils.isEmpty(ipaddr)) {
            ps.add(criteriaBuilder.like(root.get("ipaddr"), "%" + ipaddr + "%"));
        }
        if (!StringUtils.isEmpty(loginLocation)) {
            ps.add(criteriaBuilder.like(root.get("loginLocation"), "%" + loginLocation + "%"));
        }
        if (!StringUtils.isEmpty(browser)) {
            ps.add(criteriaBuilder.like(root.get("browser"), "%" + browser + "%"));
        }
        if (!StringUtils.isEmpty(os)) {
            ps.add(criteriaBuilder.like(root.get("os"), "%" + os + "%"));
        }
        if (!StringUtils.isEmpty(status)) {
            ps.add(criteriaBuilder.like(root.get("status"), "%" + status + "%"));
        }
        if (!StringUtils.isEmpty(startTimestamp)) {
            ps.add(criteriaBuilder.like(root.get("startTimestamp"), "%" + startTimestamp + "%"));
        }
        if (!StringUtils.isEmpty(lastAccessTime)) {
            ps.add(criteriaBuilder.like(root.get("lastAccessTime"), "%" + lastAccessTime + "%"));
        }
        if (!StringUtils.isEmpty(expireTime)) {
            ps.add(criteriaBuilder.like(root.get("expireTime"), "%" + expireTime + "%"));
        }

        criteriaQuery = criteriaQuery.where(ps.toArray(new Predicate[ps.size()]));

        return criteriaQuery.getRestriction();
    }

}